package com.jason.bbs.user.service.impl;

import java.sql.Timestamp;
import java.util.Date;

import javax.annotation.Resource;

import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import com.jason.bbs.bean.user.UserInfo;
import com.jason.bbs.user.dao.UserDao;
import com.jason.bbs.user.service.UserService;

@Service
public class UserServiceImpl implements UserService {
	@Resource
	private UserDao userDao;

	@Override
	public UserInfo addUserInfo(UserInfo user) {
		if (user.getStatus() == null) {
			user.setStatus(1);
		}
		user.setCreateTime(new Timestamp(new Date().getTime()));
		return userDao.save(user);
	}

	@Override
	public void deleteUserInfo(String id) {
		if (id != null && id.matches("\\d+")) {
			UserInfo user = userDao.findById(Integer.valueOf(id)).get();
			user.setStatus(0);
			userDao.save(user);
		}
	}

	@SuppressWarnings("deprecation")
	@Override
	public Page<UserInfo> find(UserInfo user, int pageSize, int pageNumber) {
		if(user.getStatus()==null) {
			user.setStatus(1);
		}
		Example<UserInfo> example=Example.of(user);
		Pageable pageable=new PageRequest(pageNumber, pageSize, Direction.DESC,"id");
		return userDao.findAll(example, pageable);
	}

	@Override
	public boolean exitisById(String id) {
		UserInfo user=new UserInfo();
		user.setId(Integer.valueOf(id));
		user.setStatus(1);
		return userDao.exists(Example.of(user));
	}

}
